package com.sj.purchase.modules.cost.web;

import com.sj.purchase.modules.cost.repository.order.StockAdjustRepository;
import com.sj.purchase.modules.cost.views.stock.AdjustOrderVo;
import com.sj.purchase.common.utils.FileOperateUtil;
import com.sj.purchase.modules.cost.domain.order.AdjustOrder;
import com.sj.purchase.manager.SkuInfo;
import com.sj.purchase.manager.SkuInfoManager;
import com.sj.purchase.util.DateUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * Created by 宋开宗 on 2017/7/27 0027.
 */
@RestController
@RequestMapping("/api/stock/adjust")
public class StockAdjustController {
	@Autowired
	private SkuInfoManager skuInfoManager;
	@Autowired
	private StockAdjustRepository repository;

	@GetMapping
	public ResponseEntity list(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
							   @DateTimeFormat(pattern = "yyyy-MM-dd") Date end, Pageable pageable) {
		Page<AdjustOrder> page = repository.findByCreateDateBetween(DateUtils.getStartOfDay(start), DateUtils.getStartOfDay(end), pageable);
		List<AdjustOrderVo> list = new ArrayList<>(pageable.getPageSize());
		page.getContent().forEach(order -> {
			SkuInfo skuInfo = skuInfoManager.getSkuInfo(order.getSkuNo());
			AdjustOrderVo adjustOrderVo = new AdjustOrderVo(order.getAdjustmentNo(),
					skuInfo.getMid(),
					skuInfo.getMname(),
					order.getSkuNo(),
					skuInfo.getSkuname(),
					order.getAdjustedQty(),
					order.getCreateUser(),
					order.getCreateDate());
			list.add(adjustOrderVo);
		});
		Map<String, Object> map = new HashMap<>();
		map.put("content", list);
		map.put("totalPages", page.getTotalPages());
		map.put("totalElements", page.getTotalElements());
		return ResponseEntity.ok(map);

	}


	@GetMapping("/excel")
	public void excel(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
					  @DateTimeFormat(pattern = "yyyy-MM-dd") Date end,
					  HttpServletRequest request,
					  HttpServletResponse response) throws Exception {
		Page<AdjustOrder> page = repository.findByCreateDateBetween(DateUtils.getStartOfDay(start), DateUtils.getStartOfDay(end), null);
		List<AdjustOrderVo> list = new ArrayList<>();
		page.getContent().forEach(order -> {
			SkuInfo skuInfo = skuInfoManager.getSkuInfo(order.getSkuNo());
			AdjustOrderVo adjustOrderVo = new AdjustOrderVo(order.getAdjustmentNo(),
					skuInfo.getMid(),
					skuInfo.getMname(),
					order.getSkuNo(),
					skuInfo.getSkuname(),
					order.getAdjustedQty(),
					order.getCreateUser(),
					order.getCreateDate());
			list.add(adjustOrderVo);
		});
		Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), AdjustOrderVo.class, list);
		try {
			FileOperateUtil.download(request, response, "库存调整单.xls", workbook);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
